home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / newext < prev    next >
Encoding:
Text File  |  1997-08-26  |  1.5 KB  |  74 lines

  1. :
  2. # newext: change filename extension
  3. # @(#) newext.sh 1.1 93/04/13
  4. # 90/06/06 john h. dubois iii (john@armory.com)
  5. # 90/11/14 changed ksh-specific code to hybrid: if running under Bourne,
  6. #          uses expr instead of ksh builtin ops.  Removed SYSV specific code.
  7. # 91/08/06 added -t option
  8. # 92/11/06 made earlier code actually work!
  9. # 93/04/13 If no filenames given, act on files in current dir
  10.  
  11. name=newext
  12. usage="Usage: $name [-th] <oldext> <newext> [filename ...]"
  13.  
  14. case "$1" in
  15. -t) 
  16.     test=true
  17.     shift
  18.     ;;
  19. -h) 
  20.     echo \
  21. "$name: Rename all given files that end in oldext with newext replacing oldext.
  22. $usage
  23. If no filenames are given, all files in the current directory that end
  24. in oldext are acted on (no filename is equivalent to '*').
  25. Options:
  26. -h: Print this help.
  27. -t: Test: No action is taken except to print the mv commands that would
  28.     be executed if -t was not given."
  29.     exit 0 
  30.     ;;
  31. esac
  32.  
  33. oldext=$1
  34. newext=$2
  35.  
  36. case $# in 
  37. [01])
  38.     echo "$usage\nUse -h for help."
  39.     exit 1
  40.     ;;
  41. 2)
  42.     shift ; shift
  43.     set -- *
  44.     ;;
  45. *)
  46.     shift ; shift
  47.     ;;
  48. esac
  49.  
  50. found=
  51.  
  52. for file
  53. do
  54.     case "$file" in
  55.     *$oldext) 
  56.     # if $PS3 set, use ksh builtins
  57.     if [ -n "$PS3" ]; then
  58.         newname="${file%$oldext}$newext"
  59.     else
  60.         newname=`expr "$file" : "\(.*\)$oldext`"$newext"
  61.     fi
  62.     if [ -n "$test" ]; then
  63.         echo mv "$file" "$newname"
  64.     else
  65.         mv "$file" "$newname"
  66.     fi
  67.     found=true;;
  68.     esac
  69. done
  70.  
  71. if [ -z "$found" ]; then
  72.     echo "No files ending in \"$oldext\"."
  73. fi
  74.